﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FunctionPoint.Common;
using System.Collections;
using FunctionPoint.Utility;
using FunctionPoint.Services;
using Microsoft.VisualStudio.Shell;

namespace FunctionPoint.UserControls
{
    /// <summary>
    /// Interaction logic for FunctionPointExplorerTreeControl.xaml
    /// </summary>
    public partial class FunctionPointExplorerTreeControl : UserControl
    {

        #region Constructors -----------------------------------

        public FunctionPointExplorerTreeControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Backing Variables --------------------------------------

        private Point _startPoint;
        private IFunctionPointManagerService _functionPointManagementService = null;

        #endregion

        #region DependencyProperty Registration -------------------------------------

        public static readonly DependencyProperty AllowQueryExpansionProperty =
            DependencyProperty.Register("AllowQueryExpansion", typeof(bool), typeof(FunctionPointExplorerTreeControl));

        public static readonly DependencyProperty AllowFilterProperty =
            DependencyProperty.Register("AllowFilter", typeof(bool), typeof(FunctionPointExplorerTreeControl));

        public static readonly DependencyProperty SelectedQueryProperty =
            DependencyProperty.Register("SelectedQuery", typeof(WorkItemQuery), typeof(FunctionPointExplorerTreeControl));

        #endregion

        #region DependencyProperty Impl -------------------------------------

        public WorkItemQuery SelectedQuery
        {
            get
            {
                return (WorkItemQuery)GetValue(SelectedQueryProperty);
            }
        }
        public string SelectedQueryName
        {

            get
            {
                return SelectedQuery.Name;
            }

        }
        public bool AllowQueryExpansion
        {
            get
            {
                return (bool)GetValue(AllowQueryExpansionProperty);
            }
            set
            {
                SetValue(AllowQueryExpansionProperty, value);
            }
        }
        public bool AllowFilter
        {
            get
            {
                return (bool)GetValue(AllowFilterProperty);

            }
            set
            {
                SetValue(AllowFilterProperty, value);
            }
        }

        #endregion

        #region Properties -----------------------------------------------------

        public IFunctionPointManagerService FunctionPointManagementServce
        {
            get
            {
                if (_functionPointManagementService == null)
                    _functionPointManagementService = ServiceProvider.GlobalProvider.GetService(typeof(SFunctionPointManagerService)) as IFunctionPointManagerService;

                return _functionPointManagementService;
            }

        }

        #endregion

        #region TreeView Events ------------------------------------------------------
        
        private void TreeView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }
        private void TreeView_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePos = e.GetPosition(null);
            Vector diff = _startPoint - mousePos;
            TreeView treeView = sender as TreeView;
            WorkItem workItem = treeView.SelectedItem as WorkItem;

            if (e.LeftButton == MouseButtonState.Pressed &&
               (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                if (workItem != null)
                {
                    DataObject dragData = new DataObject(Common.Constants.DROPHANDLER_DROPFORMAT_WORKITEMTYPE, workItem);
                    DragDrop.DoDragDrop(treeView, dragData, DragDropEffects.All);
                }
            }

        }

        #endregion
    
    }
}
